#include "stdio.h"
#include <iostream>
#include <fstream>
#include <string>
#include <string.h>
#include <memory>
#include <vector>

#include "../dataengine/VibModelFileManager.h"
#include "../dataengine/VorestHtmlReader.h"
#include "../geom/PolyModel.h"
#include "../geom/PolyObject.h"
#include "../UnitTest++/src/UnitTest++.h"

using namespace std;

SUITE(HtmlReaderTests)
{
	TEST(ReadHtmlTest)
	{
		dataengine::VorestHtmlReader htmlReader("tests/vorestHtml1.html");
		CHECK(htmlReader.canLoadFile());
		vector<dataengine::VorestTreeInfo> vec = htmlReader.readFile();
		CHECK(vec.size() == 63);

		dataengine::VorestTreeInfo &vi = vec[0];

		CHECK_EQUAL(vi.treeID() , 1);
		CHECK_CLOSE(vi.position().x , -180.0, 0.001);
		CHECK_CLOSE(vi.position().y , -62.0, 0.001);
		CHECK_CLOSE(vi.position().z ,  0.0, 0.001);
		CHECK_CLOSE(vi.diameter() , 88.301201, 0.001);
		CHECK_CLOSE(vi.length() , 86.000000, 0.001);
		CHECK_CLOSE(vi.color().dx , 157, 0.001);
		CHECK_CLOSE(vi.color().dy , 240, 0.001);
		CHECK_CLOSE(vi.color().dz , 145, 0.001);

		CHECK_CLOSE(vi.rotation() , 54.350029, 0.001);
		CHECK_CLOSE(vi.area() , 6101.00, 0.001);
		CHECK_EQUAL(vi.speciesID() , 1);
		CHECK_CLOSE(vi.treeHeight() , 1.0, 0.001);
		CHECK_CLOSE(vi.globalHeight() , 0.0, 0.001);
		CHECK_EQUAL(vi.isAlive() , true);
	}
}

SUITE(ModelLoadingTest)
{
    TEST(CheckIfVibModelIsLoadable)
    {
        dataengine::VibModelFileManager vibloader("tests/arbolTest.vib");
		CHECK(vibloader.canLoadFile());
    	dataengine::VibModelFileManager bogusvibloader("tests/bogustestmodel.vib");
		CHECK(!(bogusvibloader.canLoadFile()));
    }
	TEST(LoadModel)
	{
        dataengine::VibModelFileManager vibloader("tests/arbolTest.vib");
		auto_ptr< geom::PolyObject >pm = vibloader.loadFile();
	}	
	
	TEST(LoadAndSave)
	{
        dataengine::VibModelFileManager vibloader("tests/arbolTest.vib");
		auto_ptr< geom::PolyObject >pm = vibloader.loadFile();

        dataengine::VibModelFileManager vibwriter("tests/testwrittenmodel.vib");
		vibwriter.saveToFile(*pm);
	}	
}

